import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import WelCome from '../views/WelCome.vue'
import Permission from '../views/Permission.vue'
import Role from '../views/Role.vue'
import TestTree from '../views/TestTree.vue'
import User from '../views/User.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/tree',
    name: 'TestTree',
    component: TestTree,
  },
  {
    path: '/home',
    redirect:"/"
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
        {path:"/",name:"WelCome",component:WelCome},
        {path:"/permission",name:"Permission",component:Permission},
        {path:"/role",name:"Role",component:Role},
        {path:"/managerlist",name:"User",component:User},
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let  isAuthenticated = sessionStorage.getItem("jwttoken")
  if (to.name !== 'Login' && !isAuthenticated) next({ name: 'Login' })
  // 如果用户未能验证身份，则 `next` 会被调用两次
  //放行
  next()
})

export default router
